/*
 * @Description: Rolin's code edit
 * @Author: Rolin-Code
 * @Date: 2021-07-01 08:43:11
 * @LastEditors: Rolin
 * @Code-Function-What do you want to do: 
 * @FilePath: \创新创业案例-区块链即时通讯d:\Project\WuhuServiceSystem\wuhuflight\js\seat-select.js
 */
var isChoose = false;
var row = 0;
var col = 0;
var id = "";

var sc;
var price = 88.88; //票价


$(document).ready(function () {
    getName();
    var $cart = $('#selected-seats'), //座位区
        $counter = $('#counter'), //票数
        $total = $('#total'); //总计金额

    sc = $('#seat-map').seatCharts({
        map: [ //座位图
            'aaa_____aaa',
            'aaa_____aaa',
            'aaa_aaa_aaa',
            'aaa_aaa_aaa',
            'aaa_aaa_aaa',
            'aaa_aaa_aaa',
            'aaa_aaa_aaa',
            'aaa_aaa_aaa',
            'aaa_aaa_aaa',
            'aaa_aaa_aaa',
            'aaa_aaa_aaa',
            'aaa_aaa_aaa',
            'aaa_aaa_aaa',
            '____aaa_aaa',
            '____aaa_aaa',
            'aaa_aaa_aaa',
            'aaa_aaa_aaa',
            '____aaa____'
        ],
        naming: {
            top: true,
            rows: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18],
            columns: ['A', 'B', 'C', '', 'D', 'E', 'F', '', 'G', 'H', 'I'],
            getLabel: function (character, row, column) {
                return column;
            }
        },
        legend: { //定义图例
            node: $('#legend'),
            items: [
                ['a', 'available', '可选座'],
                ['a', 'unavailable', '已售出'],
                ['a', 'recommend', '推荐座位']
            ]
        },
        click: function () { //点击事件
            if (this.status() == 'available') { //可选座
                $('<li>' + (this.settings.row + 1) + '行' + this.settings.label + '座</li>')
                    .attr('id', 'cart-item-' + this.settings.id)
                    .data('seatId', this.settings.id)
                    .appendTo($cart);
                if(id != ""){
                    sc.get(id).status("available");
                    $('#cart-item-' + id).remove();
                }
                $counter.text(sc.find('selected').length+1);
                $total.text(recalculateTotal(sc)+price);
                
                col = this.settings.label;
                row = this.settings.row+1;
                id = this.settings.id;


                return 'selected';
            } else if (this.status() == 'selected') { //已选中
                //更新数量
                $counter.text(sc.find('selected').length - 1);
                //更新总计
                $total.text(recalculateTotal(sc) - price);
                id="";
                //删除已预订座位
                $('#cart-item-' + this.settings.id).remove();

                //可选座
                return 'available';
            } else if (this.status() == 'unavailable') { //已售出
                return 'unavailable';
            } else {
                return this.style();
            }
        }
    });
    //已售出的座位
    //sc.get(['4_J','3_A']).status('unavailable');
    //推荐座位
    //$('#4_A').addClass("recommend");
    //$('#4_B').addClass("recommend");
    loadStop(sc);
    //推荐窗边
    $('#windows-side').on("click", function () {
        if ($('#windows-side').prop('checked')) {
            for (var i = 1; i <= 18; i++) {
                if (i >= 6 && i <= 13) continue;
                $('#' + i + '_A').addClass("recommend");
                $('#' + i + '_I').addClass("recommend");
            }
        } else {
            for (var i = 1; i <= 18; i++) {
                if (i >= 6 && i <= 13) continue;
                $('#' + i + '_A').removeClass("recommend");
                $('#' + i + '_I').removeClass("recommend");
            }
        }
    });



});

//计算总金额
function recalculateTotal(sc) {
    var total = 0;
    sc.find('selected').each(function () {
        total += price;
    });

    return total;
}

function recomSeat() {
    switch ($('#type').val()) {
        case "none":
            clearClass();
            break;
        case "window":
            clearClass();
            for (var i = 1; i <= 18; i++) {
                if (i >= 6 && i <= 13) continue;
                $('#' + i + '_A').addClass("recommend");
                $('#' + i + '_I').addClass("recommend");
            }
            break;
        case "passrode":
            clearClass();
            for (var i = 1; i <= 18; i++) {
                $('#' + i + '_C').addClass("recommend");
                $('#' + i + '_D').addClass("recommend");
                $('#' + i + '_F').addClass("recommend");
                $('#' + i + '_G').addClass("recommend");
            }
            break;
    }
}

function clearClass() {
    for (var i = 1; i <= 18; i++) {
        for (var j = 65; j <= 73; j++) {
            var char = String.fromCharCode(j);
            $("#" + i + "_" + char).removeClass("recommend");
        }
    }
}

function submitSeat() {
    $.ajax({
        url:"/wss/seat/buy-seat.do",
        type:"POST",
        contentType:"application/json;charset=utf-8",
        data:JSON.stringify({
            so_seat_no_row:row,
            so_seat_no_col:col,
        }),
        dataType:"json",
        success:function (data) {
            alert(data.message);
        }
    });

}